<template>
  <div class="login">
    <van-nav-bar left-arrow @click-left="backspacing"></van-nav-bar>
    <div class="form_login">
      <van-form ref="form">
        <h3>短信登录</h3>
        <van-field
          name="mobile"
          v-model="form.mobile"
          :rules="rules.mobile"
          placeholder="请输入手机号"
        ></van-field>
        <van-field
          v-model="form.code"
          :rules="rules.code"
          placeholder="请输入验证码"
        >
          <template #button>
            <div class="code_on" v-if="countDown === 60" @click="sendCode">
              发送验证码
            </div>
            <div class="code_down" v-else>{{ countDown }}秒后,重新发送</div>
          </template>
        </van-field>
        <van-button @click="submit">登录</van-button>
      </van-form>
    </div>
  </div>
</template>

<script>
import { getCode, getUserLogin } from '@/api/userlogin.js'
export default {
  name: 'Login',
  data () {
    return {
      form: {
        mobile: '13200000000',
        code: '246810'
      },
      rules: {
        mobile: [
          { required: true, trigger: 'onChange' },
          {
            validator: value => {
              const result = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              return result.test(value)
            },
            message: '手机号格式错误',
            trigger: 'onChange'
          }
        ],
        code: [
          { required: true, trigger: 'onChange' },
          {
            validator: value => {
              const result = /^\d{6}$/
              return result.test(value)
            },
            message: '请输入6位验证码',
            trigger: 'onChange'
          }
        ]
      },
      countDown: 60, // 倒计时
      intervalId: '' // 计时器id
    }
  },
  // 全局验证--登录
  methods: {
    submit () {
      this.$refs.form.validate().then(async () => {
        const res = await getUserLogin(this.form)
        // 存储token
        this.$store.commit('user/setTokenInfo', res.data.token)
        const address = this.$route.query.address || '/'
        this.$router.push(address)
        // console.log(res)
      })
    },
    // 局部验证--发送验证码
    sendCode () {
      this.$refs.form
        .validate('mobile')
        .then(async () => {
          this.$toast.success('验证码已发送')
          this.countDown--
          this.intervalId = setInterval(() => {
            console.log(this.countDown)
            this.countDown--
            if (this.countDown <= 0) {
              clearInterval(this.intervalId)
              this.countDown = 60
            }
          }, 1000)
          await getCode(this.form.mobile)
        })
        .catch(() => {
          this.$toast.fail('手机号错误')
        })
    },
    // 回退
    backspacing () {
      this.$router.push('/')
    }
  },
  beforeDestroy () {
    // 页面跳转后停止计时器
    clearInterval(this.intervalId)
  }
}
</script>

<style lang="less" scoped>
.login {
  ::v-deep .van-nav-bar .van-icon {
    color: #999;
  }
  .form_login {
    padding: 20px;
    h3 {
      font-size: 22px;
      font-weight: normal;
      padding: 50px 16px;
    }
    .code_on {
      color: #000;
    }
    .code_down {
      color: #999;
    }
    .van-button {
      width: 100%;
      color: #fff;
      background: linear-gradient(to right, #fe4f4e, #fc6627);
      margin-top: 50px;
    }
  }
}
</style>
